<template>
  <!-- :on-success  组件内部封装好，用于拿数据 -->
  <upload-excel :on-success="handleSuccess" />
</template>

<script>
import { importEpy } from '@/api/employees'
export default {
  name: 'Import',
  data() {
    return {
      type: ''
    }
  },
  created() {
    this.type = this.$route.query.type
  },
  methods: {
    async handleSuccess({ results }) {
      const userRelations = {
        '入职日期': 'timeOfEntry',
        '手机号': 'mobile',
        '姓名': 'username',
        '转正日期': 'correctionTime',
        '工号': 'workNumber'
      }
      const newResults = []
      results.forEach(one => {
        const newOne = {}
        for (const cn_key in one) {
          let val = one[cn_key] // 属性值
          const en_key = userRelations[cn_key] // 英文属性名
          if (en_key === 'timeOfEntry' || en_key === 'correctionTime') {
            val = this.formatExcelDate(val, '-')
          }
          newOne[en_key] = val
        }
        newResults.push(newOne)
      })
      if (this.type === '21676876876') {
        await importEpy(newResults)
        this.$message.success('批量导入员工成功')
        this.$router.back()
      }
    },
    // 处理时间
    formatExcelDate(numb, format) {
      const time = new Date((numb) * 24 * 3600000 + 1) // 毫秒
      time.setYear(time.getFullYear() - 70)
      const year = time.getFullYear() + ''
      const month = time.getMonth() + 1 + ''
      const date = time.getDate() - 1 + ''
      if (format && format.length === 1) {
        return year + format + (month < 10 ? '0' + month : month) + format + (date < 10 ? '0' + date : date)
      }
      return year + (month < 10 ? '0' + month : month) + (date < 10 ? '0' + date : date)
    }
  }
}
</script>

<style>

</style>
